package hoge.memoriesalbum.get;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class ImageOverlay extends Overlay {
	final Paint paint = new Paint();

	private float density;
	private float textSize = 0;

	private Projection proj;
	private GeoPoint geoPoint = null;
	private Point point;
	
	private Bitmap bmp = null;
	private String txt = null;
	
	public ImageOverlay() {
	}
	
	public ImageOverlay(GeoPoint gp, Bitmap bmp, String txt) {
		this.geoPoint = gp;
		this.bmp = bmp;
		this.txt = txt;
	}

	public void draw(Canvas canvas, MapView mmv, boolean shadow) {
		if (!shadow) {
			if ((geoPoint != null) && (bmp != null)) {
				// 解像度を取得
				density = mmv.getContext().getResources().getDisplayMetrics().density;
				if (textSize == 0.0) {
					textSize = paint.getTextSize();
				}
				paint.setTextSize(textSize * density);
				
				proj = mmv.getProjection();
				point = proj.toPixels(geoPoint, null);
				
				paint.setStyle(Paint.Style.FILL);
				paint.setAntiAlias(true);
				// aspect 考慮せず　center 表示
				int width = mmv.getWidth();
				int height = mmv.getHeight();
				Rect src = new Rect();
				src.left = 0;
				src.top = 0;
				src.right = bmp.getWidth();
				src.bottom = bmp.getHeight();
				Rect dst = new Rect();
				dst.left = (int)(width * 0.25);
				dst.top = (int)(height * 0.25);
				dst.right = (int)(width * 0.75);
				dst.bottom = (int)(height * 0.75);
				canvas.drawBitmap(bmp, src, dst, paint);
				// cneter から表示
				if (txt != null) {
		            paint.setColor(Color.RED);          
					canvas.drawText(txt, point.x, point.y, paint);
				}
			}
		}
	}
	
	public void setGeoPoint(GeoPoint gp)
	{
		this.geoPoint = gp;
	}
	
	public void setBitmap(Bitmap bmp)
	{
		this.bmp = bmp;
	}
	
	public void setText(String txt)
	{
		this.txt = txt;
	}
}
